/*
 * @ClassName fun.dart
 * @Description 公共的一些方法
 * @Author SuPeiSen
 * @Date 2020/7/13 18:00
 */

import 'dart:convert';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:flutter_su/common/input_check.dart';
import 'package:flutter_su/config/app_config.dart';
import 'package:flutter_su/enum/language_type.dart';
import 'package:flutter_su/provider/language_provider.dart';
import 'package:provider/provider.dart';


/// 存放一些静态的公共方法
class SFunCommon {
//  /// 单例对象
//  static FunCommon _instance;
//
//  /// 内部构造方法，可避免外部暴露构造函数，进行实例化
//  FunCommon._internal();
//
//  /// 工厂构造方法，这里使用命名构造函数方式进行声明
//  factory FunCommon.getInstance() => _getInstance();
//
//  /// 获取单例内部方法
//  static _getInstance() {
//    // 只能有一个实例
//    if (_instance == null) {
//      _instance = FunCommon._internal();
//    }
//    return _instance;
//  }

  // 调试输出
  static void debugPrint(Object object, {String msg}) {
    if (AppSetting.IS_DEBUG) {
      Map map = {
        'data': object,
        'msg': msg ?? "打印变量",
        'time': DateTime.now(),
      };
      print(map);
    }
  }

  /// 根据UI图来设配高度
  static num height(double height) {
    return ScreenUtil().setHeight(height);
  }

  /// 根据UI图来设配宽度
  static num width(double width) {
    return ScreenUtil().setWidth(width);
  }

  /// 根据UI设配字体
  static num fontSize(double size) {
    return ScreenUtil().setSp(size);
  }

  /// 字体样式
  static TextStyle fontStyle(Color color,
      {double fontSize, TextDecoration decoration, FontWeight fontWeight}) {
    return TextStyle(
      fontSize: fontSize != null
          ? ScreenUtil().setSp(fontSize)
          : ScreenUtil().setSp(10),
      color: color,
      decoration: decoration ?? TextDecoration.none,
      fontWeight: fontWeight ?? FontWeight.w400,
    );
  }

  /// Provider对应Consumer的widget
  static Widget text(TextStyle textStyle, {@required ContentType contentType, @required String key}){
    return Consumer<LanguageProvider>(
      builder: (_, LanguageProvider provider, child) => Text(
        provider.getText(contentType: contentType, key: key) ?? "",
        style: textStyle,
//        maxLines: 2,
      )
    );
  }
}


// 存放一些非静态的公共方法
class FunCommon {

  // 时间戳转时间
  String timestampToDate(int timestamp, {String sign = "-", bool onlyDate = true}) {
    var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
    String minute = date.minute < 10 ? "0${date.minute}" : "${date.minute}";
    String second = date.second < 10 ? "0${date.second}" : "${date.second}";
    String month = date.month < 10 ? "0${date.month}" : "${date.month}";
    String day = date.day < 10 ? "0${date.day}" : "${date.day}";
    if(onlyDate){
      String str =
          "${date.year}$sign$month$sign$day ${date.hour}:$minute:$second";
      return str;
    }
    String millisecond = date.millisecond < 10 ? "0${date.millisecond}" : "${date.millisecond}";
    String microsecond = date.microsecond < 10 ? "0${date.microsecond}" : "${date.microsecond}";
    String str =
        "${date.year}$sign$month$sign$day${date.hour}$minute$second$sign$millisecond$sign$microsecond";
    return str;
  }

  /// string转list
  List<String> stringToList(String str) {
    List<String> list = <String>[];
    for (var value in JsonDecoder().convert(str)) {
      print(value);
      list.add(value);
    }
    return list;
  }

  /// list转换string
  String listToString(List<String> list) {
    return JsonEncoder().convert(list);
  }

  // 校验邮箱
  String checkEmail(String email, {String message = '邮箱格式不正确'}) {
    Map<String, List<Map<String, dynamic>>> rules = {
      "email": [
        {
          ValidateInput.PATTERN: Pattern.emailPattern,
          ValidateInput.MESSAGE: message,
        },
      ],
    };
    Map<String, dynamic> params = {
      "email": email,
    };
    var validate = ValidateInput(
      rules: rules,
      params: params,
    );
    return validate.message;
  }


}